package com.ysx.oa.delay.service.impl;

import com.ysx.oa.clazz.bean.ClazzStudentTeacherBean;
import com.ysx.oa.clazz.dao.ClazzDao;
import com.ysx.oa.clazz.dao.ClazzStudentTeacherDao;
import com.ysx.oa.delay.bean.DelayBean;
import com.ysx.oa.delay.dao.DelayDao;
import com.ysx.oa.delay.service.DelayService;
import com.ysx.oa.delay.utils.GetDateTime;
import com.ysx.oa.student.dao.StudentDao;
import com.ysx.oa.user.bean.UserBean;
import com.ysx.oa.user.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class DelayServiceImpl implements DelayService {

    @Autowired
    private DelayDao dao;

    @Autowired
    private ClazzDao clazzDao;

    @Autowired
    private ClazzStudentTeacherDao clazzStudentTeacherDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private StudentDao studentDao;





    @Override
    public int add(List<DelayBean> beanList,Integer loginUserId) {

        int flag = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for(DelayBean bean : beanList){

            // 录入的信息不能为空
            if(bean.getUserId()==null||bean.getDelayDate()==null){
                flag = 1;
                break;
            }

            // 格式化迟到时间
            String delayDate = bean.getDelayDate();

            try {
                Date parse = sdf.parse(delayDate);
                String format = sdf.format(parse);
                bean.setDelayDate(format);

            } catch (ParseException e) {
                flag =1;
                e.printStackTrace();
                break;
            }

        }
        if(flag == 0){
            // 录入的信息不能重复
            for(int index =0 ;index<beanList.size()-1;index++){
                for(int inner =index+1 ;inner<beanList.size();inner++){
                    DelayBean before =beanList.get(index);
                    DelayBean next =beanList.get(inner);
                    if(before.getUserId().equals(next.getUserId())&&before.getDelayDate().equals(next.getDelayDate())){
                        flag =1;
                        break;
                    }
                }
            }
        }

        // 得到班级
        ClazzStudentTeacherBean clazzStudentTeacherBean = new ClazzStudentTeacherBean();
        clazzStudentTeacherBean.setUserId(loginUserId);
        ClazzStudentTeacherBean clazz = clazzStudentTeacherDao.selectOne(clazzStudentTeacherBean);
        Integer teacherId =0;
        Integer clazzId = 0;
        if(clazz==null){
            flag=1;
        }else {

            // 得到该班级的老师
            clazzId = clazz.getClazzId();
            List<UserBean> userBeanList = clazzDao.findTeacherMassageByClazzId(clazzId);
            UserBean userBean = userBeanList.get(0);
            if(userBean==null){
                flag=1;
            }else {
                teacherId =userBean.getId();
            }
        }



        // 录入的数据和数据库的数据不能重复
        if(flag ==1||dao.getDelayRecordCount(beanList)!=0){
            return 0;
        }

        for(DelayBean bean : beanList){
            bean.setClazzId(clazzId);
            bean.setTeacherId(teacherId);
            String nowTime = GetDateTime.nowTime();
            bean.setCreateTime(nowTime);
            bean.setLastUpdateTime(nowTime);
        }


        return dao.addDelayRecord(beanList);
    }


    @Override
    public int updateDelayRecord(DelayBean delayBean, String newDelayDate) {

        String oldDelayDate =delayBean.getDelayDate();
        delayBean.setDelayDate(newDelayDate);
        delayBean.setDeleteFlag("n");
        if(dao.selectCount(delayBean)!=0){
            return 0;
        }
        delayBean.setDelayDate(oldDelayDate);
        String nowTime = GetDateTime.nowTime();
        delayBean.setLastUpdateTime(nowTime);
        return dao.updateDelayRecord(delayBean,newDelayDate);
    }

    @Override
    public int deleteDelayRecord(DelayBean delayBean) {
        String nowTime = GetDateTime.nowTime();
        delayBean.setLastUpdateTime(nowTime);
        return dao.deleteDelayRecord(delayBean);
    }

    @Override
    public List<DelayBean> findDelayRecord(DelayBean delayBean) {

        List<DelayBean> delayRecord = dao.findDelayRecord(delayBean);


        return delayRecord;
    }

    @Override
    public List<UserBean> addInit(Integer loginUserId) {


        List<UserBean> studentList =null;
        // 得到班级
        ClazzStudentTeacherBean clazzStudentTeacherBean = new ClazzStudentTeacherBean();
        clazzStudentTeacherBean.setUserId(loginUserId);
        ClazzStudentTeacherBean clazz = clazzStudentTeacherDao.selectOne(clazzStudentTeacherBean);

        Integer clazzId = 0;
        if(clazz!=null){

            // 得到该班级的老师
            clazzId = clazz.getClazzId();
            studentList = studentDao.findStudentListByClassId(clazzId);
        }


        return studentList;
    }


}
